package com.zds.support.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.viewbinding.ViewBinding

/**
 * CommonRecyclerAdapter
 *
 *@Author zc
 *@Version V1.0
 **/
abstract class CommonRecyclerAdapter<V : ViewBinding, T> : BaseRecyclerAdapter<CommonRecyclerAdapter.CommonViewHolder<V>> {
    private val data: ArrayList<T>

    constructor(context: Context, data: ArrayList<T>) : this(context, data, true)

    constructor(context: Context, data: ArrayList<T>, showEmptyView: Boolean) : super(context, showEmptyView) {
        this.data = data
    }

    constructor(context: Context, data: ArrayList<T>, @DrawableRes mEmptyImageViewResId: Int, @StringRes emptyMsgRes: Int)
            : super(context, mEmptyImageViewResId, emptyMsgRes) {
        this.data = data
    }

    constructor(context: Context, data: ArrayList<T>, @DrawableRes mEmptyImageViewResId: Int, emptyMsg: String)
            : super(context, mEmptyImageViewResId, emptyMsg) {
        this.data = data
    }

    constructor(context: Context, data: ArrayList<T>, emptyMsgRes: Int)
            : super(context, emptyMsgRes) {
        this.data = data
    }

    override fun getAdapterItemCount(): Int = data.size

    override fun onCreateAdapterViewHolder(parent: ViewGroup?, viewType: Int): CommonRecyclerAdapter.CommonViewHolder<V> {
        val viewBinding = getViewBinding(parent)
        return CommonViewHolder(mContext, viewBinding)
    }

    override fun onBindAdapterViewHolder(holder: CommonRecyclerAdapter.CommonViewHolder<V>, position: Int) {
        val item = data.getOrNull(position)
        if (item != null) {
            convert(holder.viewBinding, item)
        }
    }

    class CommonViewHolder<V : ViewBinding> constructor(context: Context, val viewBinding: V)
        : BaseViewHolder(context, viewBinding.getRoot()) {

    }

    abstract fun getViewBinding(parent: ViewGroup?): V
    abstract fun convert(viewBinding: V, data: T)
}

